/*
 * CookController.java
 * Copyright (c) 2014, 2019, Kiri. 
 * All rights reserved.
 */
package com.onesalt.web.api;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.onesalt.common.ExceptionConstants;
import com.onesalt.domain.User;
import com.onesalt.service.UserService;
import com.onesalt.utils.JSONUtil;
import com.onesalt.web.api.request.CookRegisterRequest;
import com.onesalt.web.api.response.CookRegisterResponse;
import com.onesalt.web.api.response.Response;
import com.onesalt.web.api.response.UserResponse;

/**
 * Rest Api.
 *
 * @author kiri
 * @date 2015年4月4日
 * @version 1.0
 */
@RestController
@RequestMapping("/api/cooks")
public class CookRestController extends AbstractRestController {

	private final UserService userService;

	@Autowired
	public CookRestController(UserService userService) {
		this.userService = userService;
	}
	
	@RequestMapping(value = "/{sn}", method = RequestMethod.DELETE)
	public Response deleteCook(@PathVariable String sn) {
		int result = getUserService().getUserRepository().deleteBySn(sn);
		getLogger().debug("result = " + result);
		return Response.SUCCESS;
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST, consumes = { MediaType.APPLICATION_JSON_VALUE })
	public CookRegisterResponse register(@RequestBody CookRegisterRequest request) {
		getLogger().debug("request = " + JSONUtil.toJsonString(request));
		User user = getValidUser(request);

		if (request.getLat() != null) {
			user.setLat(request.getLat());
		}
		if (request.getLon() != null) {
			user.setLon(request.getLon());
		}
		if (StringUtils.hasText(request.getAddress())) {
			user.setAddress(request.getAddress());
		}
		if (StringUtils.hasText(request.getProvince())) {
			user.setProvince(request.getProvince());
		}
		if (StringUtils.hasText(request.getCity())) {
			user.setCity(request.getCity());
		}
		if (StringUtils.hasText(request.getDistrict())) {
			user.setDistrict(request.getDistrict());
		}
		if (StringUtils.hasText(request.getProfession())) {
			user.setProfession(request.getProfession());
		}
		if (StringUtils.hasText(request.getName())) {
			user.setName(request.getName());
		}
		if (StringUtils.hasText(request.getVitae())) {
			user.setVitae(request.getVitae());
		}
		if (StringUtils.hasText(request.getPhoto())) {
			user.setPhoto(request.getPhoto());
		}
		
		user.setCookApplyTime(new Date());
		user.setCookAuditStatus(User.CookAuditStatus.IN_PROGRESS);
		if (StringUtils.hasText(request.getSkills())) {
			user.setCookSkills(request.getSkills());
		}
		
		user = getUserService().createCook(user, request.getKitchen());
		CookRegisterResponse response = new CookRegisterResponse();
		response.setUser(user);
		response.setKitchen(user.getKitchen());
		response.setRegisterCount(user.getUserId());
		return response;
	}
	
	@RequestMapping(value = "/audit", method = RequestMethod.POST)
	public UserResponse auditCook(@RequestParam long cookId, @RequestParam String auditStatus) {
		User cook = getUserService().auditCook(cookId, auditStatus);
		if (cook == null) {
			throw ExceptionConstants.COOK_NOT_EXISTS_EXCEPTION;
		}
		UserResponse response = new UserResponse();
		response.setUser(cook);
		return response;
	}
	
	public UserService getUserService() {
		return userService;
	}
}
